<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="Hugo 0.98.0" />

  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="author" content="" />
  <meta property="og:url" content="/posts/%E7%A4%BE%E5%8C%BA%E4%B9%A6%E7%B1%8D/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/" />
  <link rel="canonical" href="/posts/%E7%A4%BE%E5%8C%BA%E4%B9%A6%E7%B1%8D/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/" /><link rel="alternate" type="application/atom+xml" href="/index.xml" title="信息管理实验室">

  <script type="application/ld+json">
  {
      "@context" : "http://schema.org",
      "@type" : "BlogPosting",
      "mainEntityOfPage": {
           "@type": "WebPage",
           "@id": "\/"
      },
      "articleSection" : "posts",
      "name" : "入门：字符与字符串",
      "headline" : "入门：字符与字符串",
      "description" : "在计算机中，二进制数字与二进制数字串构成了各种数据。而在C语言中，字符与字符串涵盖了各种形式的字符数据。\n字符 字符就是各种符号与各种文字，而在C语言中，由于字符就是数字的一种形式，所以我们可以通过数字运算循环，打印出这种“字符-数字\u0026quot;对应关系。\n在程序设计中，验证观点的最好办法就是动手制作程序，下面我们将会向您展示 C语言 中的“字符-数字\u0026quot;对应关系。\nC语言：使用 Ascil 字符集 \/* ascil.c Print the ascil table BeginnerC *\/ #include \u0026lt;stdio.h\u0026gt;int main() {  for (int i = 0; i \u0026lt; 256 ;i\u002b\u002b)  {  printf(\u0026#34;%d =\u0026gt; %c \\t\u0026#34;, i, i);  if (i % 9 == 0)  {  printf(\u0026#34;\\n\u0026#34;);  }  }  printf(\u0026#34;\\n\u0026#34;);  return 0; } 下面我们分析其中的知识点\n for 循环语句 在阅读过《入门：C语言中的逻辑与循环》之后，我们相信你一定已经了解到了 while 循环的使用方法。 事实上，C语言中的循环，殊途同归，只不过是同一样逻辑的不同表达形式。 for 循环与 while 循环的区别在于，for 循环为循环的初始化与每一次循环的收尾语句提供了位置 % 运算 求余运算可以计算出每一次整数除法之后的余数 A % B 如果结果为 0，代表 A 可以被 B 整除 printf 中的 %d %c 在 printf 函数中，%d 用于打印整数，%c 用于打印字符 \u0026lsquo;\\t\u0026rsquo; 字符 制表符，反映到我们此处的程序，可以视作“8个空格\u0026quot;  而除了上面四条的知识点之外，最重要的莫过于整数与具体字符的对应关系，我们将这种关系称呼为“字符集\u0026quot;",
      "inLanguage" : "en-US",
      "author" : "",
      "creator" : "",
      "publisher": "",
      "accountablePerson" : "",
      "copyrightHolder" : "",
      "copyrightYear" : "2023",
      "datePublished": "2023-01-30 00:00:00 \u002b0000 UTC",
      "dateModified" : "2023-01-30 00:00:00 \u002b0000 UTC",
      "url" : "\/posts\/%E7%A4%BE%E5%8C%BA%E4%B9%A6%E7%B1%8D\/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97\/%E5%85%A5%E9%97%A8%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2\/",
      "keywords" : [ "C语言入门指南","C语言","信息管理实验室", ]
  }
</script>
<title>入门：字符与字符串</title>
  <meta property="og:title" content="入门：字符与字符串" />
  <meta property="og:type" content="article" />
  <meta property="og:description" content="在计算机中，二进制数字与二进制数字串构成了各种数据。而在C语言中，字符与字符串涵盖了各种形式的字符数据。
字符 字符就是各种符号与各种文字，而在C语言中，由于字符就是数字的一种形式，所以我们可以通过数字运算循环，打印出这种“字符-数字&amp;quot;对应关系。
在程序设计中，验证观点的最好办法就是动手制作程序，下面我们将会向您展示 C语言 中的“字符-数字&amp;quot;对应关系。
C语言：使用 Ascil 字符集 /* ascil.c Print the ascil table BeginnerC */ #include &amp;lt;stdio.h&amp;gt;int main() {  for (int i = 0; i &amp;lt; 256 ;i&#43;&#43;)  {  printf(&amp;#34;%d =&amp;gt; %c \t&amp;#34;, i, i);  if (i % 9 == 0)  {  printf(&amp;#34;\n&amp;#34;);  }  }  printf(&amp;#34;\n&amp;#34;);  return 0; } 下面我们分析其中的知识点
 for 循环语句 在阅读过《入门：C语言中的逻辑与循环》之后，我们相信你一定已经了解到了 while 循环的使用方法。 事实上，C语言中的循环，殊途同归，只不过是同一样逻辑的不同表达形式。 for 循环与 while 循环的区别在于，for 循环为循环的初始化与每一次循环的收尾语句提供了位置 % 运算 求余运算可以计算出每一次整数除法之后的余数 A % B 如果结果为 0，代表 A 可以被 B 整除 printf 中的 %d %c 在 printf 函数中，%d 用于打印整数，%c 用于打印字符 &amp;lsquo;\t&amp;rsquo; 字符 制表符，反映到我们此处的程序，可以视作“8个空格&amp;quot;  而除了上面四条的知识点之外，最重要的莫过于整数与具体字符的对应关系，我们将这种关系称呼为“字符集&amp;quot;" />
  <meta name="description" content="在计算机中，二进制数字与二进制数字串构成了各种数据。而在C语言中，字符与字符串涵盖了各种形式的字符数据。
字符 字符就是各种符号与各种文字，而在C语言中，由于字符就是数字的一种形式，所以我们可以通过数字运算循环，打印出这种“字符-数字&amp;quot;对应关系。
在程序设计中，验证观点的最好办法就是动手制作程序，下面我们将会向您展示 C语言 中的“字符-数字&amp;quot;对应关系。
C语言：使用 Ascil 字符集 /* ascil.c Print the ascil table BeginnerC */ #include &amp;lt;stdio.h&amp;gt;int main() {  for (int i = 0; i &amp;lt; 256 ;i&#43;&#43;)  {  printf(&amp;#34;%d =&amp;gt; %c \t&amp;#34;, i, i);  if (i % 9 == 0)  {  printf(&amp;#34;\n&amp;#34;);  }  }  printf(&amp;#34;\n&amp;#34;);  return 0; } 下面我们分析其中的知识点
 for 循环语句 在阅读过《入门：C语言中的逻辑与循环》之后，我们相信你一定已经了解到了 while 循环的使用方法。 事实上，C语言中的循环，殊途同归，只不过是同一样逻辑的不同表达形式。 for 循环与 while 循环的区别在于，for 循环为循环的初始化与每一次循环的收尾语句提供了位置 % 运算 求余运算可以计算出每一次整数除法之后的余数 A % B 如果结果为 0，代表 A 可以被 B 整除 printf 中的 %d %c 在 printf 函数中，%d 用于打印整数，%c 用于打印字符 &amp;lsquo;\t&amp;rsquo; 字符 制表符，反映到我们此处的程序，可以视作“8个空格&amp;quot;  而除了上面四条的知识点之外，最重要的莫过于整数与具体字符的对应关系，我们将这种关系称呼为“字符集&amp;quot;" />
  <meta property="og:locale" content="zh-cn" /><meta property="og:image" content="" />
  

  
    <style>body{font-family:bree serif,sans-serif;-webkit-font-smoothing:antialiased;margin:0 20px}article{max-width:800px;margin-left:auto;margin-right:auto}a{color:#000;text-decoration:none}a:hover{font-weight:600;text-decoration:underline}.post-ads{margin:50px 0}.markdown-body{font-size:18px;max-width:100%}.markdown-body a{text-decoration:underline;text-decoration-color:#000}.markdown-body blockquote{margin:0;padding:0 1em;color:#57606a;border-left:.25em solid #d0d7de}.markdown-body pre{padding:16px;overflow:auto;border-radius:10px}.markdown-body code{padding:.2em .4em;font-size:85%;background-color:#f6f8fa;border-radius:6px}.markdown-body pre>code{padding:0;font-size:100%;background-color:inherit;border:0}.Chinese .markdown-body{line-height:200%}.site-date-catalog{font-size:2rem}.header-title{font-size:2rem;font-weight:700;margin-top:32px;font-family:bungee shade,sans-serif}.header-title a{text-decoration:none}.header-subtitle{color:#666}.header-items{margin:10px 0}.header-item{margin:0 5px}.header-line{width:100%;border-width:2px;border-color:#482936;border-style:solid none none none}.lang-switch{font-weight:600}#posts-list{min-height:600px}.posts-line{font-size:1.2rem;margin:12px 0}.posts-categories{font-size:.8rem;margin:auto;text-align:center}.posts-category{padding:3px 0;border:#000 2px solid;border-radius:5px}.site-footer{margin-top:50px}.site-footer-item{margin-right:12px}.post-content img{max-width:100%;display:block;margin-right:auto;margin-top:12px}.post-header{margin-bottom:50px}.post-title{font-size:2rem;font-weight:600}.post-tags{display:inline-block;font-weight:600;padding:2px 5px;margin-right:6px;margin-top:4px;border:#000 2px solid;border-radius:5px}.post-date{font-weight:800;font-style:italic}.post-author{float:right;font-weight:600}.page-content{min-height:60%}.post-content{margin-bottom:50px}.post-content p{hyphens:auto;line-height:1.8;text-justify:ideographic;margin-bottom:1em}.related-content{border-width:3px;border-style:solid;border-color:#000;padding:0 10px;margin-bottom:50px;margin-top:100px}.related-content li{margin:5px 0}.taxonomy-term{font-size:3rem}.gallery-img{text-align:center}.gallery-img span{text-align:center}.gallery-img-desc{font-size:.8em;font-weight:800}@media screen and (max-width:600px){.header-title,.header-subtitle,.header-items{text-align:center}.posts-line{font-size:16px}.markdown-body{font-size:16px}.post-title{font-size:2rem}.post-content p{letter-spacing:.05em}}@media screen and (max-width:48em){.posts-category{display:none}}</style>
  
  
    <style>.container,.container-fluid{margin-right:auto;margin-left:auto}.container-fluid{padding-right:2rem;padding-left:2rem}.row{box-sizing:border-box;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-flex:0;-ms-flex:0 1 auto;flex:initial;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-ms-flex-direction:row;flex-direction:row;-ms-flex-wrap:wrap;flex-wrap:wrap;margin-right:-.5rem;margin-left:-.5rem}.row.reverse{-webkit-box-orient:horizontal;-webkit-box-direction:reverse;-ms-flex-direction:row-reverse;flex-direction:row-reverse}.col.reverse{-webkit-box-orient:vertical;-webkit-box-direction:reverse;-ms-flex-direction:column-reverse;flex-direction:column-reverse}.col-xs,.col-xs-1,.col-xs-10,.col-xs-11,.col-xs-12,.col-xs-2,.col-xs-3,.col-xs-4,.col-xs-5,.col-xs-6,.col-xs-7,.col-xs-8,.col-xs-9,.col-xs-offset-0,.col-xs-offset-1,.col-xs-offset-10,.col-xs-offset-11,.col-xs-offset-12,.col-xs-offset-2,.col-xs-offset-3,.col-xs-offset-4,.col-xs-offset-5,.col-xs-offset-6,.col-xs-offset-7,.col-xs-offset-8,.col-xs-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-xs{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-xs-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-xs-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-xs-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-xs-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-xs-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-xs-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-xs-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-xs-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-xs-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-xs-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-xs-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-xs-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-xs-offset-0{margin-left:0}.col-xs-offset-1{margin-left:8.33333333%}.col-xs-offset-2{margin-left:16.66666667%}.col-xs-offset-3{margin-left:25%}.col-xs-offset-4{margin-left:33.33333333%}.col-xs-offset-5{margin-left:41.66666667%}.col-xs-offset-6{margin-left:50%}.col-xs-offset-7{margin-left:58.33333333%}.col-xs-offset-8{margin-left:66.66666667%}.col-xs-offset-9{margin-left:75%}.col-xs-offset-10{margin-left:83.33333333%}.col-xs-offset-11{margin-left:91.66666667%}.start-xs{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-xs{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-xs{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-xs{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-xs{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-xs{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-xs{-ms-flex-pack:distribute;justify-content:space-around}.between-xs{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-xs{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-xs{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}@media only screen and (min-width:48em){.container{width:49rem}.col-sm,.col-sm-1,.col-sm-10,.col-sm-11,.col-sm-12,.col-sm-2,.col-sm-3,.col-sm-4,.col-sm-5,.col-sm-6,.col-sm-7,.col-sm-8,.col-sm-9,.col-sm-offset-0,.col-sm-offset-1,.col-sm-offset-10,.col-sm-offset-11,.col-sm-offset-12,.col-sm-offset-2,.col-sm-offset-3,.col-sm-offset-4,.col-sm-offset-5,.col-sm-offset-6,.col-sm-offset-7,.col-sm-offset-8,.col-sm-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-sm{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-sm-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-sm-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-sm-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-sm-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-sm-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-sm-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-sm-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-sm-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-sm-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-sm-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-sm-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-sm-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-sm-offset-0{margin-left:0}.col-sm-offset-1{margin-left:8.33333333%}.col-sm-offset-2{margin-left:16.66666667%}.col-sm-offset-3{margin-left:25%}.col-sm-offset-4{margin-left:33.33333333%}.col-sm-offset-5{margin-left:41.66666667%}.col-sm-offset-6{margin-left:50%}.col-sm-offset-7{margin-left:58.33333333%}.col-sm-offset-8{margin-left:66.66666667%}.col-sm-offset-9{margin-left:75%}.col-sm-offset-10{margin-left:83.33333333%}.col-sm-offset-11{margin-left:91.66666667%}.start-sm{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-sm{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-sm{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-sm{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-sm{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-sm{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-sm{-ms-flex-pack:distribute;justify-content:space-around}.between-sm{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-sm{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-sm{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}@media only screen and (min-width:64em){.container{width:65rem}.col-md,.col-md-1,.col-md-10,.col-md-11,.col-md-12,.col-md-2,.col-md-3,.col-md-4,.col-md-5,.col-md-6,.col-md-7,.col-md-8,.col-md-9,.col-md-offset-0,.col-md-offset-1,.col-md-offset-10,.col-md-offset-11,.col-md-offset-12,.col-md-offset-2,.col-md-offset-3,.col-md-offset-4,.col-md-offset-5,.col-md-offset-6,.col-md-offset-7,.col-md-offset-8,.col-md-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-md{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-md-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-md-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-md-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-md-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-md-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-md-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-md-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-md-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-md-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-md-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-md-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-md-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-md-offset-0{margin-left:0}.col-md-offset-1{margin-left:8.33333333%}.col-md-offset-2{margin-left:16.66666667%}.col-md-offset-3{margin-left:25%}.col-md-offset-4{margin-left:33.33333333%}.col-md-offset-5{margin-left:41.66666667%}.col-md-offset-6{margin-left:50%}.col-md-offset-7{margin-left:58.33333333%}.col-md-offset-8{margin-left:66.66666667%}.col-md-offset-9{margin-left:75%}.col-md-offset-10{margin-left:83.33333333%}.col-md-offset-11{margin-left:91.66666667%}.start-md{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-md{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-md{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-md{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-md{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-md{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-md{-ms-flex-pack:distribute;justify-content:space-around}.between-md{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-md{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-md{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}@media only screen and (min-width:75em){.container{width:76rem}.col-lg,.col-lg-1,.col-lg-10,.col-lg-11,.col-lg-12,.col-lg-2,.col-lg-3,.col-lg-4,.col-lg-5,.col-lg-6,.col-lg-7,.col-lg-8,.col-lg-9,.col-lg-offset-0,.col-lg-offset-1,.col-lg-offset-10,.col-lg-offset-11,.col-lg-offset-12,.col-lg-offset-2,.col-lg-offset-3,.col-lg-offset-4,.col-lg-offset-5,.col-lg-offset-6,.col-lg-offset-7,.col-lg-offset-8,.col-lg-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-lg{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-lg-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-lg-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-lg-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-lg-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-lg-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-lg-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-lg-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-lg-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-lg-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-lg-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-lg-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-lg-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-lg-offset-0{margin-left:0}.col-lg-offset-1{margin-left:8.33333333%}.col-lg-offset-2{margin-left:16.66666667%}.col-lg-offset-3{margin-left:25%}.col-lg-offset-4{margin-left:33.33333333%}.col-lg-offset-5{margin-left:41.66666667%}.col-lg-offset-6{margin-left:50%}.col-lg-offset-7{margin-left:58.33333333%}.col-lg-offset-8{margin-left:66.66666667%}.col-lg-offset-9{margin-left:75%}.col-lg-offset-10{margin-left:83.33333333%}.col-lg-offset-11{margin-left:91.66666667%}.start-lg{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-lg{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-lg{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-lg{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-lg{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-lg{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-lg{-ms-flex-pack:distribute;justify-content:space-around}.between-lg{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-lg{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-lg{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}</style>
  

  

  <link href="/index.xml" rel="alternate" type="application/rss+xml"
    title="信息管理实验室">
  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css?family=Bree+Serif|Bungee+Shade" rel="stylesheet">
  
  

  
  
</head>


<body>
  <article class="post " id="article">
    <div class="row">
      <div class="col-xs-12">
        <div class="site-header">
          
<header>
  <div class="header-title">
    <a href="/"
      >信息管理实验室</a
    >
  </div>
  <div class="header-subtitle"></div>
</header>
<div class="row end-md center-xs header-items">
  
  <div class="header-item">
    <a href="/posts/%e4%bd%a0%e5%a5%bd%e6%9c%8b%e5%8f%8b/" target="_blank">About</a>
  </div>
  
  <div class="header-item">
    <a href="/tags/%e6%88%91%e4%bb%ac%e7%9a%84%e4%bc%99%e4%bc%b4/" target="_blank">Friends</a>
  </div>
  
  <div class="header-item">
    <a href="/tags/%e7%a4%be%e5%8c%ba%e4%b9%a6%e7%b1%8d/" target="_blank">Books</a>
  </div>
  
</div>
<div class="row end-xs">
   
</div>
<div class="header-line"></div>

        </div>
        <header class="post-header">
          <h1 class="post-title">入门：字符与字符串</h1>
          
          <div class="row post-desc">
            <div class="col-xs-6">
              
              <time class="post-date">
                2023-01-30
              </time>
              
              
              
              <a href="/series/c%e8%af%ad%e8%a8%80%e5%85%a5%e9%97%a8%e6%8c%87%e5%8d%97/"><strong>&nbsp;《C语言入门指南》</strong></a>
              
              
              <br>
            </div>
          </div>
          <div class="row end-xs">
            
              <a href="/author/%e6%95%ac%e7%ab%a0/"><strong>&nbsp;@敬章</strong></a>        
            
          </div>
          
        </header>

        <div class="post-content markdown-body">
          
          <p>在计算机中，二进制数字与二进制数字串构成了各种数据。而在C语言中，字符与字符串涵盖了各种形式的字符数据。</p>
<h2 id="字符">字符</h2>
<p>字符就是各种符号与各种文字，而在C语言中，由于字符就是数字的一种形式，所以我们可以通过数字运算循环，打印出这种“字符-数字&quot;对应关系。</p>
<p>在程序设计中，验证观点的最好办法就是动手制作程序，下面我们将会向您展示 C语言 中的“字符-数字&quot;对应关系。</p>
<h3 id="c语言使用-ascil-字符集">C语言：使用 Ascil 字符集</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    ascil.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Print the ascil table
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; i <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">256</span> ;i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;%d =&gt; %c </span><span style="color:#ae81ff">\t</span><span style="color:#e6db74">&#34;</span>, i, i);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (i <span style="color:#f92672">%</span> <span style="color:#ae81ff">9</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">0</span>)
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            printf(<span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672711907384.png" alt="1672711907384"></p>
<p>下面我们分析其中的知识点</p>
<ol>
<li>for 循环语句
在阅读过《入门：C语言中的逻辑与循环》之后，我们相信你一定已经了解到了 while 循环的使用方法。
事实上，C语言中的循环，殊途同归，只不过是同一样逻辑的不同表达形式。
for 循环与 while 循环的区别在于，for 循环为循环的初始化与每一次循环的收尾语句提供了位置</li>
<li>% 运算
求余运算可以计算出每一次<strong>整数除法</strong>之后的余数
A % B 如果结果为 0，代表 A 可以被 B 整除</li>
<li>printf 中的 %d %c
在 printf 函数中，%d 用于打印整数，%c 用于打印字符</li>
<li>&lsquo;\t&rsquo; 字符
制表符，反映到我们此处的程序，可以视作“8个空格&quot;</li>
</ol>
<p>而除了上面四条的知识点之外，最重要的莫过于整数与具体字符的对应关系，我们将这种关系称呼为“字符集&quot;</p>
<p>（C语言使用 Ascil 字符集，转换关系由上面我们制作的程序所体现）</p>
<h3 id="putchar--getchar-函数">putchar &amp; getchar 函数</h3>
<p>在 C语言 中，putchar 函数可以用于打印字符，如您所见</p>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672716958984.png" alt="1672716958984"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    putchar.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Use the putchar function
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    putchar(<span style="color:#e6db74">&#39;c&#39;</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>而 getchar 函数可以用来读取字符</p>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672720001605.png" alt="1672720001605"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    getchar.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Use the getchar function
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> c;
</span></span><span style="display:flex;"><span>    c <span style="color:#f92672">=</span> getchar();
</span></span><span style="display:flex;"><span>    putchar(c);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="字符的识别">字符的识别</h3>
<p>C语言提供了有用的组件来帮助我们处理各种字符，这一组有用的组件包含在 ctype.h 标准库中。</p>
<p>比如，我们可以制作一个简单的字符识别小程序，它可以判断输入字符的类型。</p>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672719503523.png" alt="1672719503523"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    ctype.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Judge the type of char
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;ctype.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> c;
</span></span><span style="display:flex;"><span>    c <span style="color:#f92672">=</span> getchar();
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> (isalpha(c))
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;%c is a letter</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, c);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (isupper(c))
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            printf(<span style="color:#e6db74">&#34;Upper Letter</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span> (islower(c))
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            printf(<span style="color:#e6db74">&#34;Lower Letter</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span> (isdigit(c))
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;%c is a number</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, c);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">else</span>
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;Unknow</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="字符的处理">字符的处理</h3>
<p>在对字符的类型进行判断之外，我们还可以对字符本身进行转换。</p>
<p>ctype.h 也为我们提供了这方面的组件。</p>
<blockquote>
<p><a href="https://cplusplus.com/reference/cctype/tolower/"><strong>tolower</strong></a> 与 <a href="https://cplusplus.com/reference/cctype/toupper/"><strong>toupper</strong></a> 能够帮助我们将字母的大小写进行转换</p>
</blockquote>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672719849122.png" alt="1672719849122"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    alpha.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Use toupper &amp; tolower
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;ctype.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> c;
</span></span><span style="display:flex;"><span>    c <span style="color:#f92672">=</span> getchar();
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> (isalpha(c))
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (islower(c))
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            putchar(toupper(c));
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">else</span>
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            putchar(tolower(c));
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">else</span>
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;Not a alpha.</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="拓展中文的表示">拓展：中文的表示</h3>
<p>早期的计算机，因为硬件条件与软件设计的限制，最初的字符集只能够用于表示字母与简单的符号，而对于其它形式的文字则无能为力。</p>
<p>而伴随着计算机硬件与软件的发展，有许多其它的字符集得以提出，用于展现其它的文字。</p>
<p>其中，GBK字库用于表示中文，为了兼容 Ascil 字符集合，GBK 字库使用 “单双字节变长编码，单字节编码完全兼容ASCII字符编码，而中文部分采用双字节编码。&quot;</p>
<p>这种单双字节变长编码的做法，我们可以采用如下的形式进行验证：</p>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672712977790.png" alt="1672712977790"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    char_chinese.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Test the chinese in C language
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> c <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;好&#39;</span>;
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d =&gt; %c</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, c, c);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>程序的执行没能符合我们的期待，原因显而易见，汉字占用两个字节，而一般的 Ascil 字符仅仅占用 1 个字节，因此自然不能用于表示汉字。</p>
<p>这种情况称之为“溢出&quot;，解决的办法在于拿出足够承载的空间。</p>
<p>在 C语言 中，标准库 wchar.h 提供了用于处理诸如汉字之类占用多字节的字符的函数与类型，我们称之为“宽字符函数库&quot;</p>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672716017927.png" alt="1672716017927"></p>
<p><a href="https://en.cppreference.com/w/c/header">C语言参考材料 (由全球C/C++ 社区维护）</a>上提供了与之有关的信息，在这里，由于宽字符相关的知识过于繁琐与复杂，我们选择不继续深入阐述。</p>
<p>值得注意的是，当下流行的字符集被称为 UTF-8 字符集，较之于 GBK，它具有更好的通用性。</p>
<h2 id="字符串">字符串</h2>
<h3 id="字符串就是字符数组">字符串就是字符数组</h3>
<p>由于字符串的本质就是字符的集合，从C语言的语法层面而言，是一个数组。</p>
<blockquote>
<p>数组就是对相同类型变量的一个集合</p>
</blockquote>
<p>如下面所展示</p>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672717145329.png" alt="1672717145329"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    string_array.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Print the string
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> string[] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;C Language&#34;</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;i <span style="color:#f92672">&lt;</span> <span style="color:#66d9ef">sizeof</span>(string);i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        putchar(string[i]);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="字符数组就是数字流">字符数组就是数字流</h3>
<p>在前文中我们发现，字符就是C语言中数字的另一种表示形式，因此对于字符串，我们当然也可以将他们视作为多个数字。</p>
<p>事实上，上面的程序可以改写为：</p>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672732995756.png" alt="1672732995756"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    string_array_int.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Print the string
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> string[] <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;C&#39;</span>, <span style="color:#e6db74">&#39; &#39;</span>, <span style="color:#e6db74">&#39;L&#39;</span>, <span style="color:#e6db74">&#39;a&#39;</span>, <span style="color:#e6db74">&#39;n&#39;</span>, <span style="color:#e6db74">&#39;g&#39;</span>, <span style="color:#e6db74">&#39;u&#39;</span>, <span style="color:#e6db74">&#39;a&#39;</span>, <span style="color:#e6db74">&#39;g&#39;</span>, <span style="color:#e6db74">&#39;e&#39;</span>, <span style="color:#e6db74">&#39;\0&#39;</span>};
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;i <span style="color:#f92672">&lt;</span> <span style="color:#66d9ef">sizeof</span>(string) <span style="color:#f92672">/</span> <span style="color:#66d9ef">sizeof</span>(string[<span style="color:#ae81ff">0</span>]);i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        putchar(string[i]);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>我们可以很明显地发现与第一个程序相比，这个程序变得明显地复杂，具体体现为如下：</p>
<ol>
<li>我们没有使用 &ldquo;C Language&rdquo; 进行赋值
这是因为，int 类型数组终究代表整数类型数组，它并不是 C语言中内置用于处理字符串类型数据的类型，因此我们不能使用字符类型的语法糖进行处理（语法糖就是将某一项问题的解决用更简单的方式表示）
只能老老实实使用数组元素赋值的语法</li>
<li>&ldquo;C Language&rdquo; 之外，多了一个 &lsquo;\0&rsquo;
在 C语言 中，所有的可以正常使用的字符串都需要以 &lsquo;\0&rsquo; 字符作为收尾，换而言之，我们字符串的储存空间实际上“比我们见到的要大一点&quot;
我们可以编写程序验证这一点</li>
<li>for 循环语句的条件中，sizeof(string) 需要除 sizeof(string[0])
这是因为，在我们使用 char 数据类型的时候，每一个元素只占用一个字节，这就意味着，sizeof(string)是多少，就有多少个元素。
而第二个程序之中，因为 int 数据类型往往占用多个字节空间（往往是4个），如果我们还要使用之前的思路进行处理，势必出现问题。
所以我们必须要将 sizeof(string) 计算出的总储存空间，除以单个元素占用的储存空间，计算出元素的个数（元素个数 * 单个元素所用空间 = 总储存空间）</li>
</ol>
<p>作为对 2 的验证，我们可以将 程序一 改写为如下</p>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672734032569.png" alt="1672734032569"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    string_array_ascil_code.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Print the array ascil code
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> string[] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;C Language&#34;</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;i <span style="color:#f92672">&lt;</span> <span style="color:#66d9ef">sizeof</span>(string);i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;%d =&gt; %c</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, string[i], string[i]);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>很明显发现，最后的元素就是 &lsquo;\0&rsquo;，也就是数字0</p>
<h3 id="用数字流的思路解决一些字符串问题">用数字流的思路，解决一些字符串问题</h3>
<h4 id="字符串的比较">字符串的比较</h4>
<p>我们可以用数字流的思路解决一些实际的问题，在这里，我们以字符串比较为案例</p>
<p>字符串的比较实际上就是对每一个元素所代表的数字进行比较，据此我们写出如下程序</p>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672734896890.png" alt="1672734896890"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    string_compare.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Compare the string
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> string_1[<span style="color:#ae81ff">1024</span>] <span style="color:#f92672">=</span> {};
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> string_2[<span style="color:#ae81ff">1024</span>] <span style="color:#f92672">=</span> {};
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> flag <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    scanf(<span style="color:#e6db74">&#34;%s&#34;</span>, string_1);
</span></span><span style="display:flex;"><span>    scanf(<span style="color:#e6db74">&#34;%s&#34;</span>, string_2);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;i <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">1024</span>;i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (string_1[i] <span style="color:#f92672">!=</span> string_2[i])
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            flag <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">break</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> (flag)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;Not Equal</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">else</span>
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;Equal</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>可以发现，尽管这个程序能够顺利地进行字符串的比较，但还是存在着许多缺陷，其中，最重要的就有两个</p>
<ol>
<li>对字符串的比较，存在长度限制</li>
<li>忽略了参与比较的字符串可能存在比较的空间问题，容易造成溢出</li>
</ol>
<p>因此，有比较对这一问题进行改进，由此我们提出如下方案</p>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672735895180.png" alt="1672735895180"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    string_compare_final.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Compare the string
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    (Final Version)
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> string_1[<span style="color:#ae81ff">1024</span>] <span style="color:#f92672">=</span> {};
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> string_2[<span style="color:#ae81ff">12</span>] <span style="color:#f92672">=</span> {};
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> flag <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    scanf(<span style="color:#e6db74">&#34;%s&#34;</span>, string_1);
</span></span><span style="display:flex;"><span>    scanf(<span style="color:#e6db74">&#34;%s&#34;</span>, string_2);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;string_1[i] <span style="color:#f92672">&amp;&amp;</span> string_2[i];i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (string_1[i] <span style="color:#f92672">!=</span> string_2[i])
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            flag <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">break</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> (flag)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;Not Equal</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">else</span>
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;Equal</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这套方案之中，我们使用 字符串结束标志 作为结束的条件（&amp;&amp; 逻辑运算中，参与运算的两个变量中的一个为 0 时，结果为 0）</p>
<p>由此解决了长度的限制.</p>
<h4 id="计算字符串的长度">计算字符串的长度</h4>
<p>用同样的方式，我们可以制作一个字符串长度统计程序</p>
<p><img src="image/%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/1672736419389.png" alt="1672736419389"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    string_length.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Calc the length of string
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> string[<span style="color:#ae81ff">1024</span>] <span style="color:#f92672">=</span> {};
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> length <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    scanf(<span style="color:#e6db74">&#34;%s&#34;</span>, string);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (length <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;string[length];length<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>        ;
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;The length is %d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, length);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="c语言中的字符串组件库">C语言中的字符串组件库</h2>
<p>C语言 string.h 标准库提供了许多有用的字符串函数，我们可以使用他们解决各种有用的问题。</p>
<p>比如，字符串的比较与字符串的长度计算，就可以使用 strcmp 与 strlen 函数进行解决。</p>

        </div>

        <div class="row middle-xs">
          <div class="col-xs-12">
            
            <div class="post-tags">
              <a href="/tags/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/">
                C语言入门指南
              </a>
            </div>
            
            <div class="post-tags">
              <a href="/tags/c%E8%AF%AD%E8%A8%80/">
                C语言
              </a>
            </div>
            
            <div class="post-tags">
              <a href="/tags/%E4%BF%A1%E6%81%AF%E7%AE%A1%E7%90%86%E5%AE%9E%E9%AA%8C%E5%AE%A4/">
                信息管理实验室
              </a>
            </div>
            
          </div>
        </div>
        
          <div class="row">
            <div class="col-xs-12">
              
            </div>
          </div>

          

<div class="related-content">
  <h3>Related Posts</h3>
  <ul>
    
    <li><a href="/posts/%E7%A4%BE%E5%8C%BA%E4%B9%A6%E7%B1%8D/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8c%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%9A%84%E9%80%BB%E8%BE%91%E4%B8%8E%E5%BE%AA%E7%8E%AF/">入门：C语言中的逻辑与循环</a></li>
    
    <li><a href="/posts/%E7%A4%BE%E5%8C%BA%E4%B9%A6%E7%B1%8D/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E5%87%BD%E6%95%B0/">入门：函数</a></li>
    
    <li><a href="/posts/%E7%A4%BE%E5%8C%BA%E4%B9%A6%E7%B1%8D/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E5%8F%98%E9%87%8F/">入门：变量</a></li>
    
  </ul>

</div>


          
          
          <div style="height: 50px;"></div>
          
        

        <div class="site-footer">
  
  
</div>

      </div>
    </div>
  </article>

  

<script>
  
  
    
    
  
</script>

  

</body>

</html>